package com.yunfei.practice_mybatis.web.controller;

import com.yunfei.practice_mybatis.web.dao.ZhangyunfeiAccountingRecordMapper;
import com.yunfei.practice_mybatis.web.dataobject.ZhangyunfeiAccountingRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: yunfei
 * @Date: 14:31
 */
@RestController
public class AccountingRecordController {
    @Autowired
    ZhangyunfeiAccountingRecordMapper ZhangyunfeiAccountingRecordMapper;

    public boolean isExist(Long id) {
        boolean flag = true;
        List<ZhangyunfeiAccountingRecord> s = ZhangyunfeiAccountingRecordMapper.selectAll();
        Map<Long, ZhangyunfeiAccountingRecord> map = new HashMap<>();
        for (ZhangyunfeiAccountingRecord item : s) {
            map.put(item.getId(), item);
        }
        if (!map.containsKey(id)) {
//            return "数据库中没有这个主键";
            flag = false;
        }
        return flag;


    }

    @RequestMapping(path = "/insert")
    //对数据增加操作
    public String insert(ZhangyunfeiAccountingRecord accountingRecord) {
        Date date = new Date();
        accountingRecord.setGmtModified(date);
        ZhangyunfeiAccountingRecordMapper.insert(accountingRecord);
        return "success??";
    }

    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") Long id) {
        if (ZhangyunfeiAccountingRecordMapper.selectByPrimaryKey(id)!=null) {
            ZhangyunfeiAccountingRecordMapper.deleteByPrimaryKey(id);
            return "success";
        } else {
            return "这个主键都没有,你删除啥呢";
        }


    }

    @RequestMapping(path = "/selectall")
//    对数据查找全部操作
    public List<ZhangyunfeiAccountingRecord> selectAll() {

        List<ZhangyunfeiAccountingRecord> ZhangyunfeiAccountingRecords = ZhangyunfeiAccountingRecordMapper.selectAll();

        return ZhangyunfeiAccountingRecords;


    }

    @RequestMapping(path = "/querybyrecordtime")
    public String select(@RequestParam("recordTime") String recordTime) {
        StringBuilder stringBuilder = new StringBuilder();
        for (ZhangyunfeiAccountingRecord accountingRecord : ZhangyunfeiAccountingRecordMapper.selectAll()
        ) {
            if (accountingRecord.getRecordTime() == recordTime) {
//                stringBuilder.append(accountingRecord.getId() + accountingRecord.getCategory() +
//                        accountingRecord.getGmtModified() + accountingRecord.getGmtCreated() +
//                        accountingRecord.getRecordTime() +
//                        accountingRecord.getAmount() + accountingRecord.getType() + accountingRecord.getDesc());
                System.out.println(accountingRecord);
            }
        }

        return stringBuilder.toString();
    }

    @RequestMapping(path = "/querybyid")
    public List query(@RequestParam("id") Long id) {
//        ZhangyunfeiAccountingRecord accountingRecord = ZhangyunfeiAccountingRecordMapper.selectByPrimaryKey(id);
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append(accountingRecord.getId() + accountingRecord.getCategory() +
//                accountingRecord.getGmtModified() + accountingRecord.getGmtCreated() +
//                accountingRecord.getRecordTime() +
//                accountingRecord.getAmount() + accountingRecord.getType() + accountingRecord.getDesc());
//        return stringBuilder.toString();
         return ZhangyunfeiAccountingRecordMapper.select(id);

    }
    @RequestMapping(path="/querybuilder")
//    public String queryBuilder(@Param("recordTime") String recordTime, @Param("amount") double amount){
    public String queryBuilder(@Param("recordTime") String recordTime, @Param("amount") double amount,
                               ZhangyunfeiAccountingRecord accountingRecord){
        ZhangyunfeiAccountingRecordMapper.query(accountingRecord);
        return ZhangyunfeiAccountingRecordMapper.query(accountingRecord).toString();

    }

    @RequestMapping(path = "/update")
    //对数据修改操作
    public String update(@RequestParam("id") Long id, ZhangyunfeiAccountingRecord modifyModel) {
        if (ZhangyunfeiAccountingRecordMapper.selectByPrimaryKey(id) != null) {
            ZhangyunfeiAccountingRecordMapper.updateByPrimaryKey(modifyModel);
            return "success!!!";
        } else {
            return "没有你输入的这个主键，你想干啥？";
        }
    }
}
